package org.xqyy.eval.service.impl;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xqyy.eval.dao.DutyEvalDetailMapper;
import org.xqyy.eval.dao.ReportMapper;
import org.xqyy.eval.excelmodel.DutyDetailInfo;
import org.xqyy.eval.excelmodel.EvalDutyInfo;
import org.xqyy.eval.excelmodel.RankBean;
import org.xqyy.eval.model.DutyFile;
import org.xqyy.eval.service.IReportService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表导出service
 */
@Service
public class ReportService implements IReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private DutyEvalDetailMapper dutyEvalDetailMapper;
    /**
     * 查询排名信息
     * @param Date 日期字符串
     * @param dutyType 科室类型
     * @return
     */
    public List<RankBean> selectRank(String Date,String dutyType) {
        List<RankBean> rankBeans = new ArrayList<>();
        List<HashMap> mapList = null;
        if ("职能科室".equals(dutyType)){
            mapList = reportMapper.zhinengpaiming(Date);
        }else if ("临床科室".equals(dutyType)){
            mapList = reportMapper.lichuangpaiming(Date);
        }else {
            mapList = reportMapper.yijipaiming(Date);
        }

        for (HashMap hashMap : mapList) {
            RankBean rankBean = new RankBean();
            rankBean.setDutyName(String.valueOf(hashMap.get("被评价科室名称")) );
            rankBean.setRanking(Integer.parseInt(hashMap.get("排名").toString()) );
            rankBean.setScore(Float.parseFloat(hashMap.get("总分").toString()));
            rankBeans.add(rankBean);
        }
        return rankBeans;
    }

    /**
     * 查询科室填报情况
     *
     * @param Date     日期字符串
     *
     * @return
     */
    @Override
    public List<DutyFile> selectDutyFile(String Date) {
        return reportMapper.dutyFile(Date);
    }

    /**
     * 查询科室评价明细信息
     *
     * @param Date     日期字符串
     * @param dutyType 科室类型
     * @return
     */
    @Override
    public List<EvalDutyInfo> selectDutyEvalDetail(String Date, String dutyType) {
        return dutyEvalDetailMapper.selectDutyEvalDetail(Date,dutyType);
    }

    /**
     * 科室评价明细数据初始化动态表头
     * @param dutytype 科室类别
     * @return
     */
    @Override
    public List<ExcelExportEntity> dutyEvalDetailTableHead(String dutytype) {
        List<ExcelExportEntity> colList = new ArrayList<>();
        ExcelExportEntity colEntity = new ExcelExportEntity("被评价科室", "evalduty");
        colEntity.setNeedMerge(true);
        colList.add(colEntity);

        ExcelExportEntity deliColGroup = new ExcelExportEntity("评价科室", "duty");
        List<ExcelExportEntity> deliColList = new ArrayList<ExcelExportEntity>();
        deliColList.add(new ExcelExportEntity("评价科室名称", "dutyname"));
        deliColList.add(new ExcelExportEntity("评价科室类别", "dutytype"));
        ExcelExportEntity excelExportEntity = new ExcelExportEntity("问题1", "question1");
        excelExportEntity.setType(10);
        deliColList.add(excelExportEntity);
        excelExportEntity = new ExcelExportEntity("问题2", "question2");
        excelExportEntity.setType(10);
        deliColList.add(excelExportEntity);
        excelExportEntity = new ExcelExportEntity("问题3", "question3");
        excelExportEntity.setType(10);
        deliColList.add(excelExportEntity);
        excelExportEntity = new ExcelExportEntity("问题4", "question4");
        excelExportEntity.setType(10);
        deliColList.add(excelExportEntity);
        excelExportEntity = new ExcelExportEntity("问题5", "question5");
        excelExportEntity.setType(10);
        deliColList.add(excelExportEntity);
        if ("医技".equals(dutytype) ){
            excelExportEntity = new ExcelExportEntity("问题6", "question6");
            excelExportEntity.setType(10);
            deliColList.add(excelExportEntity);
            excelExportEntity = new ExcelExportEntity("问题7", "question7");
            excelExportEntity.setType(10);
            deliColList.add(excelExportEntity);
            excelExportEntity = new ExcelExportEntity("问题8", "question8");
            excelExportEntity.setType(10);
            deliColList.add(excelExportEntity);
            excelExportEntity = new ExcelExportEntity("问题9", "question9");
            excelExportEntity.setType(10);
            deliColList.add(excelExportEntity);
            excelExportEntity = new ExcelExportEntity("问题10", "question10");
            excelExportEntity.setType(10);
            deliColList.add(excelExportEntity);
        }
        deliColGroup.setList(deliColList);
        colList.add(deliColGroup);
        return colList;
    }

    /**
     * 科室评价明细数据初始化动态表数据
     *
     * @param evalDutyInfos
     * @return
     */
    @Override
    public List<Map<String, Object>> dutyEvalDetailTableData(String dutytype,List<EvalDutyInfo> evalDutyInfos) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (EvalDutyInfo evalDutyInfo : evalDutyInfos) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("evalduty",evalDutyInfo.getEvaldeptname());
            List<Map<String, Object>> detailList = new ArrayList<Map<String, Object>>();
            for (DutyDetailInfo dutyDetailInfo : evalDutyInfo.getDutyDetailInfos()) {
                HashMap<String, Object> dutyDetailInfomap = new HashMap<>();
                dutyDetailInfomap.put("dutyname",dutyDetailInfo.getDeptname());
                dutyDetailInfomap.put("dutytype",dutyDetailInfo.getGroupname());
                dutyDetailInfomap.put("question1",dutyDetailInfo.getQuestion1());
                dutyDetailInfomap.put("question2",dutyDetailInfo.getQuestion2());
                dutyDetailInfomap.put("question3",dutyDetailInfo.getQuestion3());
                dutyDetailInfomap.put("question4",dutyDetailInfo.getQuestion4());
                dutyDetailInfomap.put("question5",dutyDetailInfo.getQuestion5());
                if ("医技".equals(dutytype) ){
                    if (dutyDetailInfo.getQuestion6()!= null){
                        dutyDetailInfomap.put("question6",dutyDetailInfo.getQuestion6());
                    }
                    if (dutyDetailInfo.getQuestion7()!= null){
                        dutyDetailInfomap.put("question7",dutyDetailInfo.getQuestion7());
                    }
                    if (dutyDetailInfo.getQuestion8()!= null){
                        dutyDetailInfomap.put("question8",dutyDetailInfo.getQuestion8());
                    }
                    if (dutyDetailInfo.getQuestion9()!= null){
                        dutyDetailInfomap.put("question9",dutyDetailInfo.getQuestion9());
                    }
                    if (dutyDetailInfo.getQuestion10()!= null){
                        dutyDetailInfomap.put("question10",dutyDetailInfo.getQuestion10());
                    }
                }
                detailList.add(dutyDetailInfomap);
            }
            map.put("duty",detailList);
            list.add(map);
        }
        return list;
    }
}
